package class202205.demo25.demo25;

import java.util.Arrays;

public class demo20220525 {
    /**
     * 给定一个字符串 s ，找到 它的第一个不重复的字符，并返回它的索引 。如果不存在，则返回 -1 。
     * https://leetcode.cn/problems/first-unique-character-in-a-string/
     * @param s
     * @return
     */
    public int firstUniqChar(String s) {
        if(s == null) return -1;
        int[] array = new int[26];
        for(int i = 0;i<s.length();i++){
            char ch = s.charAt(i);
            array[ch-97]++;
        }
        for(int i = 0;i<s.length();i++){
            char ch = s.charAt(i);
            if(array[ch-97]==1){
                return i;
            }
        }
        return -1;
    }


    public static void bubbleSort(int[]  array){
        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j]>array[j+1]){
                    int tmp = array[j];
                    array[j]=array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if (flg == false) break;;
        }
    }

    public static void main(String[] args) {
        int[] array = {1,2,31,14,5};
        System.out.println("逆置之前，数组为:"+ Arrays.toString(array));
        reverse(array);
        System.out.println("逆置之后，数组为:"+ Arrays.toString(array));
    }

    public static void reverse(int[] array){
        int left = 0;
        int right = array.length-1;
        int tmp = 0;
        while (left<right){
            tmp = array[left];
            array[left] = array[right];
            array[right]= tmp;
            left++;
            right--;
        }
    }
}
